using System;
using DG.Tweening;
using UnityEngine;

namespace DanMuHelper.Tools
{
    public class UIFollow3D : MonoBehaviour
    {
        [Header("偏移量")]
        public Vector3 offset;

        public Camera _cameraMain;

        public Camera CameraMain
        {
            get
            {
                if (null == _cameraMain)
                {
                    _cameraMain = Camera.main;
                }

                return _cameraMain;
            }
        }

        private CanvasGroup _canvasGroup;

        private Camera _cameraUI;
        public Transform FollowTransform;

        private RectTransform _container;
        protected RectTransform rect;

        private Vector3 _position;
        private Vector2 _convertPosition;

        private void Awake()
        {
            if (null == _cameraUI)
            {
                var go = GameObject.FindGameObjectWithTag("UICamera");
                if (go != null)
                    _cameraUI = go.GetComponent<Camera>();
            }
            rect = transform as RectTransform;
            _container = transform.parent as RectTransform;
            _canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
        }

        protected virtual void LateUpdate()
        {
            if (FollowTransform != null)
            {
                SetPosition();
            }
        }

        private void SetPosition(Transform customTrans = null)
        {
            var pW = RectTransformUtility.WorldToScreenPoint(CameraMain,
                (null != customTrans ? customTrans : FollowTransform).position);
            RectTransformUtility.ScreenPointToLocalPointInRectangle(_container, pW, _cameraUI, out _convertPosition);

            if (!(Math.Abs(rect.localPosition.x - _convertPosition.x) > 0) &&
                !(Math.Abs(rect.localPosition.y - _convertPosition.y) > 0) && rect.localPosition.z == 0) return;
            _position.x = _convertPosition.x;
            _position.y = _convertPosition.y;
            _position.z = 0;

            var localPosition = _position;

            localPosition += offset;
            rect.localPosition = localPosition;
        }

        // [Button("同步位置")]
        private void SetRectPosition(Transform customRect = null)
        {
            FollowTransform = null;
            rect.position = ((RectTransform)customRect).position;
        }

        /// <summary>
        /// 设置跟随目标
        /// </summary>
        /// <param name="target">目标</param>
        public void SetTarget(Transform target)
        {
            if (target is RectTransform)
            {
                SetRectPosition(target);
            }
            else
            {
                FollowTransform = target;
            }

            transform.DOKill(true);
            _canvasGroup.alpha = 1;
            _canvasGroup.DOFade(1, 0.3f).From(0).SetEase(Ease.OutQuart);
        }

        public void Hide()
        {
            _canvasGroup.alpha = 0;
            //gameObject.SetActive(false);
        }

        /// <summary>
        /// 设置偏移量
        /// </summary>
        /// <param name="offset">偏移量</param>
        protected void SetOffset(Vector3 offset)
        {
            this.offset = offset;
        }

        public void Show()
        {
            _canvasGroup.alpha = 1;
        }
    }
}